package miage.ecom.commandes;


import java.io.PrintStream;
import miage.ecom.Main.Shell;
import miage.ecom.Shell.ShellContext;
import miage.ecom.commandes.ShellCommand;

    

public class HelpCommandImpl implements ShellCommand
    {
    private ShellContext _context = null;
    public HelpCommandImpl(ShellContext context)
    {
    _context = context;
    }
    /**
    * renvoie le nom de la commande
    * @return
    */
    @Override
    public String getName()
    {
    String langue = (String) _context.getVar(Shell.LANGUAGE);
    if(langue.equals("fr")) {
    
    return "aide";}
    if(langue.equals("es")) {
    return "ayuda";}
    if(langue.equals("it")) {
    return "aiuto";}
    else{
    //default anglais
    return "help";
    }
    }
    /**
    * renvoie le nom de la commande
    * @return
    */
    @Override
    public String getUsage()
    {
    String langue = (String) _context.getVar(Shell.LANGUAGE);
    if(langue.equals("fr")) {
    
    return "aide";}
    if(langue.equals("es")) {
    return "ayuda";}
    if(langue.equals("it")) {
    return "aiuto";}
    else{
    //default anglais
    return "help";
    }
    }
    /**
    * renvoie la description de la commande
    * @return
    */
    @Override
    public String getShortDescription()
    {
    String langue = (String) _context.getVar(Shell.LANGUAGE);
    if(langue.equals("fr")) {
    
    return "liste une aide.";}
    if(langue.equals("es")) {
    return "la asistencia lista.";}
    if(langue.equals("it")) {
    return "assistenza lista.";}
    else{
    //default anglais
    return "assistance list.";
    }
    }
    /**
    * execute la commande
    * @param cmdline
    * @param out
    * @param err
    */
    @Override
    public void execute(String cmdline, PrintStream out, PrintStream err) throws Exception
    {
    ShellCommand[] cmds = _context.getCommands();
    String[] usage = new String[cmds.length];
    String[] desc = new String[cmds.length];
    int maxUsage = 0;
    for (int i = 0; i < cmds.length; i++)
    {
    usage[i] = cmds[i].getUsage();
    desc[i] = cmds[i].getShortDescription();
    // Just in case the command has gone away.
    if ((usage[i] != null) && (desc[i] != null))
    {
    maxUsage = Math.max(maxUsage, usage[i].length());
    }
    }
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < cmds.length; i++)
    {
    // Just in case the command has gone away.
    if ((usage[i] != null) && (desc[i] != null))
    {
    sb.delete(0, sb.length());
    for (int j = 0; j < (maxUsage - usage[i].length()); j++)
    {
    sb.append(' ');
    }
    out.println(usage[i] + sb + " - " + desc[i]);
    }
    }
    }
    } 